#define _CRT_SECURE_NO_WARNINGS 1


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        vector<int> ans;
        if (root == nullptr) return ans;

        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty())
        {
            int count = q.size();
            int temp = INT_MIN;
            while (count--)
            {
                TreeNode* f = q.front();
                q.pop();
                temp = max(temp, f->val);

                if (f->left) q.push(f->left);
                if (f->right) q.push(f->right);
            }
            ans.push_back(temp);
        }
        return ans;
    }
};